/*
 * Created on 12-11-15
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * 
 * Copyright @2012 the original author or authors.
 */
package com.baoxian.task.job;

import com.baoxian.task.job.service.ReleaseTaskService;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Main Class
 *
 * @author XiongNeng
 * @version 1.0
 * @since 12-11-15
 */
public class Main {
    private static Logger log = null;

    /**
     * 启动主函数
     *
     * @param args 配置文件路径
     */
    public static void main(String[] args) {
        String path=Main.class.getResource("/").getPath();
        if (args.length <= 0) {
            // log.error("请输入配置文件路径参数!");
            System.out.println("没有配置文件路径参数！");
            //System.exit(1);
        }else{
            path=args[0];
        }
        final String configPath = new String(path);
        // 初始化log4j配置文件
        PropertyConfigurator.configure(configPath + "log4j.properties");
        log = LoggerFactory.getLogger(Main.class);
        // 启动新的线程去执行定时任务
        new Thread(new Runnable() {
            @Override
            public void run() {
                log.info("--------------开始运行定时job---------------");
                ReleaseTaskService.instantce().startScheduler(configPath);
            }
        }).start();
        // 开始检查是否需要周期性扫描停止标志
        log.info("--------开始检查是否需要周期性扫描停止标志------------");
        try {
            Properties props = new Properties();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(new FileInputStream(configPath + "taskjobConfig.properties")));
            props.load(reader);
            reader.close();
            String shouldScan = props.getProperty("com.baoxian.task.job.scan.flag");
            if ("true".equals(shouldScan)) {
                log.info("-----------需要周期检查停止标志---------");
                scheduleCheckStop(configPath);
            }
        } catch (IOException e) {
            log.error("---------读取配置文件出错--------",e);
        }
    }

    /**
     * 周期性检查stop标志，自动停止该服务
     * @param configPath 配置文件路径
     */
    private static void scheduleCheckStop(final String configPath) {
        // 周期性检查配置文件，看是否需要停止此次服务
        Timer timer = new Timer(true);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                log.info("--------开始扫描配置文件中服务停止标识------------");
                try {
                    Properties props = new Properties();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(new FileInputStream(configPath + "taskjobConfig.properties")));
                    props.load(reader);
                    reader.close();
                    String stopFlag = props.getProperty("com.baoxian.task.job.stopflag");
                    if ("true".equals(stopFlag)) {
                        log.info("----------stop------------");
                        ReleaseTaskService.instantce().stopScheduler();
                        // 退出系统
                        System.exit(0);
                    }
                } catch (IOException e) {
                    log.error("---------读取配置文件出错--------",e);
                }
            }
        }, 60000L, 300000L);
    }
}
